<template>
  <div class="scroll-content">

    <a-typography>
      <a-typography-title class="headline"> <span class="accent">演示</span> 窗口通信</a-typography-title>
    </a-typography>
    <a-typography-paragraph>  </a-typography-paragraph>
    <a-typography-paragraph> 
      启动应用程序，首先打开主窗口，有时候也需要打开可以是子窗口。窗口之间可以依靠IPC进行通信和交互。
    </a-typography-paragraph>
    <a-typography-title :level="5">1、不同形式的窗口</a-typography-title>
    <a-typography-paragraph> 
      打开模态窗口打开后只能操作应用中的该窗口，桌面置顶窗体一般应用于一些桌面挂件，开启后窗口置于所有打开的软件顶层。
    </a-typography-paragraph>
    <a-typography-paragraph>
    <a-space :size="[10, 10]" class="actions" wrap>        
        <a-button size="middle" shape="round" type="default" @click="openWin('vue','#/about', '模态窗体', true)">模态窗体</a-button>
        <a-button size="middle" shape="round" type="default" @click="openWin('vue','#/about', '桌面置顶窗体',false,true)">桌面置顶窗体</a-button>
        <a-popover title="打开系统默认浏览器" trigger="hover">
          <template #content>需要config.windowsOption开启配置 preload.js 文件，<br /> 安全地暴露 Electron 的 shell.openExternal</template>
          <button class="btn" @click="openExternal('https://gitee.com/')">弹出外部浏览器</button>
        </a-popover>
    </a-space>
  </a-typography-paragraph>


    <a-typography-title :level="5">2、窗口控制</a-typography-title>
    <a-typography-paragraph> 
      尝试点击按钮打开不同窗体，再点击"列出窗体信息"，并尝试Close It，窗口id号是1，由于有任务驻留，关闭它以后你可以在右下角再次唤出主界面。
      <br>控制窗体关闭，主要应用在自定义窗口顶部的窗口按钮事件：最小化、最大化。
    </a-typography-paragraph>
    <a-space :size="[10, 10]" class="actions" wrap>
      <a-button size="middle" shape="round" type="dashed" @click="openWin('web', 'https://gitee.com/', '窗体A-访问gitee')">窗体A</a-button>
      <a-button size="middle" shape="round" type="dashed" @click="openWin('vue', '#/help',  '窗体B-内部vue路由页面')">窗体B</a-button>
      <a-button size="middle" shape="round" type="default" @click="openWin('html','/public/html/view_example.html', '窗体C-静态HTML文件')">窗体C</a-button>
      <a-popover title="场景说明" trigger="hover">
        <template #content>获取当前应用的所有窗体</template>
        <a-button size="middle" type="link" @click="getAllWin()">列出窗体信息</a-button>
      </a-popover>
    </a-space>

    <p style="height:20px;"> </p>

    <a-table :columns="columns" :data-source="dataList" bordered>
      <template #bodyCell="{ column, text, record }">
        <template v-if="column.dataIndex === 'title'">
          <a>{{ text }}</a>
        </template>

        <template v-else-if="column.dataIndex == 'action'">
          <a-popconfirm v-if="dataList.length" title="关闭它吗?" @confirm="closeWin(record.id)">
            <a>关闭</a>
          </a-popconfirm>
          
          | <a @click="minimizeWin(record.id)">最小化</a>
          | <a @click="expandWin(record.id)">最大化</a>
        </template>
      </template>
    </a-table>


      
   

  </div>
</template>

<script setup>
import { ipcApiRoute } from "@/api/main";
import { ipc } from "@/utils/ipcRenderer";

import { watch, ref } from "vue";
import { message } from "ant-design-vue";

import { useRoute } from "vue-router";
const route = useRoute();

const queryParameters = ref(route.query);


watch(
  () => route.query,
  (newVal) => {
    queryParameters.value = newVal;
  }
);

const columns = [
  {
    title: '窗口Id',
    dataIndex: 'id',
  }, {
    title: '窗口标题',
    dataIndex: 'title',
  },
  {
    title: '操作',
    dataIndex: 'action',
  },
];
const dataList = ref([]);
const onDelete = key => {
  dataList.value = dataList.value.filter(item => item.id !== key);
};

function getAllWin() {
  ipc.invoke(ipcApiRoute.window.getAllWindow).then((ret) => {
    console.log("所有窗口信息", ret);
    dataList.value = ret;
  });
}

function minimizeWin(id=0) {
  ipc.invoke(ipcApiRoute.window.minimizeWindow, id).then((ret) => {
    ret == "success" ? message.info(ret, 3): message.error(ret, 3)
  });
}
function closeWin(id=0) {
  ipc.invoke(ipcApiRoute.window.closeWindow, id).then((ret) => {
    ret == "success" ? onDelete(id) : message.error(ret, 3)
  });
}
function expandWin(id=0) {
  ipc.invoke(ipcApiRoute.window.expandWindow, id).then((ret) => {
    ret == "success" ? message.info(ret, 3): message.error(ret, 3)
  });
}
function openExternal(url) {
  if (window.electron && window.electron.openExternal) {
    window.electron.openExternal(url).then(() => {
      console.log('网页已成功在系统默认浏览器中打开');
    }).catch((error) => {
      console.error('打开网页时出错:', error);
    });
  } else {
    message.error('浏览器没有暴露 Electron的shell.openExternal 方法', 18)
    window.open(url);
  }
}

function openWin(type, content="", title = "", isModal = false, isOnTop = false) {
  ipc
    .invoke(ipcApiRoute.window.createWindow, {
      type: type,
      content: content || "#/help",
      windowName: "vue-window",
      windowTitle: title || "帮助页面",
      windowFrame: true,
      windowHideMenuBar: true,
      isModal: isModal,
      resizable: true, // 可调整大小
      windowAlwaysOnTop: isOnTop, //置顶显示
      isCenter: true, //屏幕居中

    })
    .then((ret) => {
      console.log("打开窗口", type, ret);
    });
}
</script>

<style lang="less" scoped>
.scroll-content {
  height: 100%;
  margin-right: 5px;
  padding: 10px 30px 30px 20px;
  box-sizing: border-box;
  overflow: auto;
}

@media (min-width: 960px) and (max-width: 1299px) {
  .scroll-content {
    padding: 20px 40px 30px 30px;
  }
}

@media (min-width: 1300px) {
  .scroll-content {
    padding: 30px 50px 30px 50px;
  }
}

.scroll-content::-webkit-scrollbar-track {
  background-color: #f1f1f1;
  border-radius: 10px;
}

.scroll-content::-webkit-scrollbar-thumb {
  background-color: #888;
  border-radius: 10px;
  border: 2px solid #ffffff;
}

.scroll-content::-webkit-scrollbar {
  width: 10px;
}
.ant-empty-normal{
  margin-block: 1px 0px!important;
}
.headline {
  font-size: 32px;
  line-height: 1.9;
  font-weight: bold;
  letter-spacing: -1.5px;
  margin: 0px auto;
}


html:not(.dark) .accent,
.dark .headline {
  background: -webkit-linear-gradient(315deg, #42d392 25%, #647eff);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
p {
  line-height: 1.8;
  margin-top: 10px;
}

.subtitle {
  line-height: 1.5;
  font-size: 22px;
  margin: 44px auto 16px;
}

.description {
  line-height: 1.5;
  font-size: 15px;
  margin: 10px auto 15px;
}

.actions {
  margin: 10px 6px;

  .btn {
    font-size: 14px;
    line-height: 14px;
    padding: 8px 15px;
    border-radius: 25px;
    display: inline-block;
    border: 0px;
    cursor: pointer;
    font-weight: 400;
    text-decoration: none;
    background: -webkit-linear-gradient(315deg, #42d392 25%, #647eff);
    :hover {
      background: -webkit-linear-gradient(115deg, #42d392 25%, #647eff);
    }
  }
}
</style>
